package com.xintonghua.bussiness.ui.user;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.widget.ConfirmDialog;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ShopLeftAdapter;
import com.xintonghua.bussiness.adapter.ShopRightAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.AddProjectTypeBean;
import com.xintonghua.bussiness.bean.GoodsItemBean;
import com.xintonghua.bussiness.ui.user.store.CartActivity;
import com.xintonghua.bussiness.util.GlideCacheUtil;
import com.xintonghua.bussiness.util.MyState;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class StorkStoreActivity extends BaseActivity {
    @BindView(R.id.lv_store_type)
    ListView lvStoreType;
    @BindView(R.id.gv_shop)
    GridView lvStoreProject;
    @BindView(R.id.iv_cart_shop)
    ImageView ivCartShop;
    @BindView(R.id.tv_jg)
    CheckBox tvJg;
    List<AddProjectTypeBean> list;
    ShopLeftAdapter adapter;
    @BindView(R.id.tv_zh)
    TextView tvZh;
    @BindView(R.id.tv_xl)
    TextView tvXl;
    private int itemId = 0;
    private boolean zh = true;
    private boolean xl = false;
    private boolean price = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_stork);
        ButterKnife.bind(this);
        initUI();

    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("进货商城");
        EventBus.getDefault().register(this);
        httpCent.stockType(this, MyState.requestOne);
        lvStoreType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        list.get(i).setCheck(true);
                    } else {
                        list.get(i).setCheck(false);
                    }
                    itemId = list.get(position).getId();
                    find(list.get(position).getId());

                }

                adapter.replace(list);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void find(int id) {
        httpCent.stockGoods(id, zh, xl, price, this, MyState.requestTwo);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                list = JSONObject.parseArray((String) data, AddProjectTypeBean.class);
                if (list != null && list.size() > 0) {
                    list.get(0).setCheck(true);
                    itemId = list.get(0).getId();
                    find(itemId);
                }
                adapter = new ShopLeftAdapter(list, this);
                lvStoreType.setAdapter(adapter);
                break;
            case MyState.requestTwo:
                List<GoodsItemBean> list = JSONObject.parseArray((String) data, GoodsItemBean.class);
                ShopRightAdapter adapter = new ShopRightAdapter(list, this);
                lvStoreProject.setAdapter(adapter);
                break;
            case MyState.requestThree:
                mToast("添加成功！");
                break;
        }
    }

    @OnClick({R.id.iv_cart_shop, R.id.tv_xl, R.id.tv_zh, R.id.ll_jg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_cart_shop:
                openActivity(CartActivity.class);
                break;
            case R.id.tv_xl:
                xl = true;
                zh = false;
                find(itemId);
                tvXl.setTextColor(ContextCompat.getColor(this, R.color.red));
                tvZh.setTextColor(ContextCompat.getColor(this, R.color.black));
                tvJg.setTextColor(ContextCompat.getColor(this, R.color.black));
                break;
            case R.id.tv_zh:
                xl = false;
                zh = true;
                find(itemId);
                tvXl.setTextColor(ContextCompat.getColor(this, R.color.black));
                tvZh.setTextColor(ContextCompat.getColor(this, R.color.red));
                tvJg.setTextColor(ContextCompat.getColor(this, R.color.black));
                break;
            case R.id.ll_jg:
                xl = false;
                zh = false;
                price = !price;
                find(itemId);
                tvJg.setChecked(price);
                tvXl.setTextColor(ContextCompat.getColor(this, R.color.black));
                tvZh.setTextColor(ContextCompat.getColor(this, R.color.black));
                tvJg.setTextColor(ContextCompat.getColor(this, R.color.red));
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(final GoodsItemBean bean) {
        ConfirmDialog.showDialog(this, "提示", "是否加入购物车？", "取消", "确定", null, new ConfirmDialog.OnRightListener() {
            @Override
            public void onClick(ConfirmDialog dialog) {
                httpCent.addCart(bean.getId(), 1, StorkStoreActivity.this, MyState.requestThree);
            }
        });

    }
}
